<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>转化数字的最小运算数</title>
</head>
<body>
  <script>
    function fn(nums, start, goal) {
      let arr = []
      let vis = new Array(1001).fill(0)
      arr.push([start, 0])
      while (arr.length) {
        let [t, dis] = arr.shift()
        for (let i of nums) {
          for (let tt of [t + i, t - i, t ^ i]) {
            if (tt === goal) {
              return dis + 1
            }
            if (tt >= 0 && tt <= 1000 && vis[tt] === 0) {
              vis[tt] = dis + 1
              arr.push([tt, dis + 1])
            }
          }
        }
      }
      return -1
    }
    console.log(fn([2, 4, 12], 2, 12))
  </script>
</body>
</html>